
*****************
* Summary Stats *
*****************
set more off
tempfile weights

use "${build}/iv_fin_lehd.dta", clear
cap gen c_evict = evicted
cap gen no_attorney = d_prose
merge 1:1 unique_id using  "${build}/uniqueid_tractwt_cwk.dta" 
keep if _merge == 3

label var earn_1_8 "Mean Quarterly Earnings"
label var anyearn_allst_1_8 "Any employment (all sates)"
label var ageatcase "Age at Case"
label var fem "Female"
label var black "Black"
label var white "White"
label var c_evict "Evicted"
label var ad_damnum "Ad Damnum"
label var joint "Joint Action"
label var hispanic "Hispanic"
label var nbrhd_pov_rate_5yr "Neighborhood Pov. Rate"
label var nbrhd_med_rent_5yr "Neighborhood Med. Rent"

*Outcomes
global person="ageatcase fem black hispanic white"
global case=" ad_damnum joint noprior no_attorney "
global neighborhood="nbrhd_pov_rate_5yr nbrhd_med_rent_5yr"
global outcomes=" earn_b8to1 anyearn_allst_b8to1 "

global bin_vars =   "fem black white hispanic joint noprior no_attorney anyearn_allst_1_8 "
global cts_vars = "ageatcase ad_damnum  earn_b8to1 earn_b8to5 earn_b12to9 nbrhd_pov_rate_5yr nbrhd_med_rent_5yr" 

foreach var of varlist fem black white hispanic noprior nbrhd_pov_rate_5yr nbrhd_med_rent_5yr {
 replace `var' = . if `var'_miss == 1
}

eststo clear
loca j = 1
gen obscount = 0
gen outcome = ""
gen states = .
gen group = ""
gen row = .

eststo sum1: estpost sum $person  ///
		$case ///
		$neighborhood ///
		$outcomes            if c_evict==1 

eststo sum2: estpost  sum $person  ///
			$case ///
			$neighborhood ///
			$outcomes    if c_evict==0 

eststo sum3: estpost  sum $person  ///
			$neighborhood ///
			$outcomes    if acs_placebo == 1 [iweight = tract_wgt]

gen SAMP = c_evict
replace SAMP = 2 if acs_placebo == 1

*****************	
*EXPORT TO LATEX*
*****************
foreach v of varlist $person ///
		$case ///
		$neighborhood ///
		$outcomes {
	label variable `v' `"\hspace{0.1cm} `: variable label `v''"'	
}	
	
esttab sum1 sum2 sum3 using "${Inputs}/Cook_lehd_sumstats.csv", main(mean %9.3f) aux(sd %9.3f) wide  replace mtitles("Evicted" "Not Evicted" "ACS SAMPLE" )

import delimited "${Inputs}/Cook_lehd_sumstats.csv", clear

gen type = "Mean" if !mi(A)
replace type = "SE" if mi(A)
replace A = A[_n-1] if mi(A)

drop if regexm(A, "Title|coefficients;|05")
drop if mi(B) & mi(C) & mi(D)

rename (A B C D) (variable evicted not_evicted acs)
reshape wide evicted not_evicted acs, i(variable) j(type) string
rename (*Mean *SE) (Mean_* SE_*)

foreach i of varlist SE* {
	replace `i' = subinstr(`i', "(", "", .)
	replace `i' = subinstr(`i', ")", "", .)
}

replace variable = "Obs" if variable=="Number of Observations"
gen sorting = 1 if variable=="Obs"
replace sorting = 0 if mi(sorting)
sort sorting variable
drop sorting
destring Mean* SE*, replace 
order variable Mean_evicted SE_evict Mean_not_evicted SE_not Mean_acs SE_acs
compress

export delimited using "${Inputs}\Cook_lehd_sumstats.csv", replace


